package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"yun_image/dao"
	"yun_image/entity"
	"yun_image/util"
)

// writeLog 记录用户请求的日志
func writeLog(context *gin.Context) {
	var log entity.Log
	token := context.GetHeader("token")
	log.Account = util.UserTokenUtil.GetAccount(token)
	log.ApiPath = context.FullPath()
	log.Time = time.Now().UnixMilli()
	log.IP = context.ClientIP()
	err := dao.CreateLog(&log)
	if err != nil {
		context.String(http.StatusInternalServerError, "")
		context.Abort()
	} else {
		context.Next()
	}
}
func UseWriteLog(router *gin.Engine) {
	router.Use(writeLog)
}
